package com.yugu.park.task;


import cn.hutool.core.date.DateUtil;
import com.yugu.park.business.service.CardInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author Noble-Six
 * @date 2024/4/25
 */
@Component
@Slf4j
public class CardTask {
    @Resource
    private CardInfoService cardInfoService;

    @Scheduled(cron = "0 0 0/5 * * ?")
    public void cardTask(){
        log.info("检测年卡有效期任务开始");
        cardInfoService.list().stream().filter(cardInfo -> (cardInfo.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isBefore(LocalDate.now()) && cardInfo.getStatus() == 1)||cardInfo.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().isEqual(LocalDate.now())).forEach(cardInfo ->{
            cardInfo.setStatus(2);
            cardInfoService.updateById(cardInfo);
            log.info(cardInfo.getName()+"过期");
        });
        log.info("检测年卡有效期任务结束");
    }

}
